package com.example.rabbitmq.exchange.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author： ygl
 * @date： 2019/06/15
 * @Description：
 * topic类型的交换机
 */
public class Producer4TopicExchange {


    public static void main(String[] args) throws Exception{
        // 1.创建ConnectionFactory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        // 2.通过链接工厂创建链接
        Connection connection = connectionFactory.newConnection();
        // 3.通过connection创建channel信道
        Channel channel = connection.createChannel();
        // 4.声明
        String exchangeName = "test_topic_exchange";
        String routingKey1 = "user.save";
        String routingKey2 = "user.update";
        String routingKey3 = "user.delete.abc";
        // 4.通过channel发送数据
        String msg = "hello rabbitmq for topic exchange message ......";
        channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
        channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
        channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
        // 5.关闭链接
        channel.close();
        connection.close();
    }
}
